package com.example.growthmuseum.postgoods;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.growthmuseum.R;

import java.io.File;

/**
 * Created by Roman on 2016/10/29.
 */
public class PostGoodsActivity extends AppCompatActivity {

    private static final int PHOTO_REQUEST_GALLERY = 2;// 从相册中选择
    private static final int PHOTO_REQUEST_CUT = 3;// 结果


    private ImageView goodsPicImg;

    private EditText goodsNameEdit;

    private EditText goodsInfoEdit;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_post_goods);

        initViews();
    }

    private void initViews() {
        goodsPicImg = (ImageView) findViewById(R.id.goods_pic_img);
        goodsInfoEdit = (EditText) findViewById(R.id.goods_info_edit);
        goodsNameEdit = (EditText) findViewById(R.id.goods_name_edit);
        Button nextBtn = (Button) findViewById(R.id.next_btn);
        nextBtn.setOnClickListener(v -> {
            if (goodsNameEdit.getText().toString().trim().isEmpty()) {
                Toast.makeText(PostGoodsActivity.this, getResources().getString(R.string.goods_name_not_null), Toast.LENGTH_SHORT).show();
            } else if (goodsInfoEdit.getText().toString().trim().isEmpty()) {
                Toast.makeText(PostGoodsActivity.this, getResources().getString(R.string.goods_info_not_null), Toast.LENGTH_SHORT).show();
            } else {
                Intent toNext = new Intent(PostGoodsActivity.this,
                        PostGoodsNextActivity.class);
                toNext.putExtra("goods_info",
                        goodsInfoEdit.getText().toString().trim());
                toNext.putExtra("goods_name",
                        goodsNameEdit.getText().toString().trim());
                startActivity(toNext);
                finish();
            }
        });
    }

    /*
     * 从相册获取
     */
    public void gallery(View view) {
        // 激活系统图库，选择一张图片
        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setType("image/*");
        // 开启一个带有返回值的Activity，请求码为PHOTO_REQUEST_GALLERY
        startActivityForResult(intent, PHOTO_REQUEST_GALLERY);
    }

    /*
  * 剪切图片
  */
    private void crop(Uri uri) {
        // 裁剪图片意图
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        intent.putExtra("crop", "true");
        // 裁剪框的比例，1：1
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        // 裁剪后输出图片的尺寸大小
        intent.putExtra("outputX", 250);
        intent.putExtra("outputY", 250);

        intent.putExtra("outputFormat", "JPEG");// 图片格式
        intent.putExtra("noFaceDetection", true);// 取消人脸识别
        intent.putExtra("return-data", true);
        // 开启一个带有返回值的Activity，请求码为PHOTO_REQUEST_CUT
        startActivityForResult(intent, PHOTO_REQUEST_CUT);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == PHOTO_REQUEST_GALLERY) {
            // 从相册返回的数据
            if (data != null) {
                // 得到图片的全路径
                Uri uri = data.getData();
                crop(uri);
            }

        }  else if (requestCode == PHOTO_REQUEST_CUT) {
            // 从剪切图片返回的数据
            if (data != null) {
                Bitmap bitmap = data.getParcelableExtra("data");
                this.goodsPicImg.setImageBitmap(bitmap);
            }
        }

        super.onActivityResult(requestCode, resultCode, data);
    }
}
